package com.study.shengsiyuan.example07;

import com.study.shengsiyuan.example07.gen_java.thrift.PersonService;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;

public class ThriftServer {
    public static void main(String[] args) throws TTransportException {
        //获取非阻塞的socket
        TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
        // agr 是配置类
        //THsHaServer : 高可用的server
        THsHaServer.Args agr = new THsHaServer.Args(socket)
                //设置工作线程的上下限
                .minWorkerThreads(2).maxWorkerThreads(4);
        //processor:处理器
        PersonService.Processor<PersonServiceImpl> processor =
                new PersonService.Processor<>(new PersonServiceImpl());
        //协议工厂
        agr.protocolFactory(new TCompactProtocol.Factory());
        //传输工厂
        agr.transportFactory(new TFramedTransport.Factory());
        //处理器工厂
        agr.processorFactory(new TProcessorFactory(processor));
        TServer server = new THsHaServer(agr);
        System.out.println("thrift server started!!!");
        //是一个阻塞方法，死循环方式
        server.serve();
    }
}
